<template>
  <div>
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="用户等级：" prop="name">
        <el-input v-model="form.name" placeholder="请输入用户等级,10字以内" maxlength="10"></el-input>
      </el-form-item>
      <el-form-item label="积分:" prop="score">
        <el-input v-model="form.score" placeholder="请输入积分,0~9999以内"></el-input>
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div class="flexend">
      <el-button type="primary" @click="onSubmit">确 认</el-button>
      <el-button @click="resetForm">重 置</el-button>
    </div>
  </div>
</template>

<script>
import { validPhone } from "@/utils/validate";
import { scoreAdd,scoreEdit } from '@/api/member'
export default {
  data() {
    return {
      form: {
        name: "",
        score: "",
        status: 1,
      },
      rules: {
        name: [{ required: true, message: "请输入用户等级", trigger: "blur" },
        // { pattern: /^\d{1,4}$/, message: '用户等级', trigger: 'blur' }
      
      ],
        score: [ { required: true, message: '分数不能为空', trigger: 'blur' },
        { pattern: /^([1-9]\d{0,3}|0)$/, message: '分数必须在0-9999之间', trigger: 'blur' }
        //  { validator: this.validateScore, trigger: 'blur' }
      
      ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }]
      },
    };
  },
  props:{
    isadd:{
       type:Boolean,
       default:true
    },
    row:{
      default:null
    }
  },
  created(){
    if(this.row){
      this.form.id = this.row.id
      this.form.name = this.row.name
      this.form.score = this.row.score
      this.form.status = this.row.status
    }
  },
  methods: {
  
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        if(this.isadd){
          scoreAdd(this.form).then(res=>{
            this.$modal.msgSuccess("添加成功");
            this.$emit('close',1)
          })
        }else{
          scoreEdit(this.form).then(res=>{
            this.$modal.msgSuccess("修改成功");
            this.$emit('close',1)
          })
        }
      });
    },

    resetForm() {
      this.$refs.form.resetFields();
    },
  },
};
</script>